using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace net.notdot.Attercop {
    [Serializable()]
    public class ChatMessage : Message, UdpMessage {
        public readonly string Text = "";
        public readonly bool IsEmote;

        static ChatMessage() { ClientInstance.AddMessageHandler<ChatMessage>(); }

        public ChatMessage(MessageAddressing addressing, string text, bool isEmote)
            : base(addressing) {
            this.Text = text;
            this.IsEmote = isEmote;
        }

        public ChatMessage(MessageAddressing addressing, string text)
            : this(addressing, text, false) {
        }

        protected ChatMessage(MessageReader reader)
            : base(reader)
        {
            reader.BeginRegion();
            reader.ReadByte();  //Version
            this.Text = reader.ReadString();
            this.IsEmote = reader.ReadBoolean();
            reader.EndRegion();
        }

        public override void Serialize(MessageWriter writer)
        {
            base.Serialize(writer);

            writer.BeginRegion();
            writer.Write((byte)0);  //Version
            writer.Write(this.Text);
            writer.Write(this.IsEmote);
            writer.EndRegion();
        }

        public virtual void Handle(ClientInstance c) {
            if (this.PeerState != PeerState.Introduced)
                return;

            c.RaiseChatEvent(this);
        }
    }
}
